package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;


/**
 *
 */
@RestController
@RequestMapping("/order")
@Slf4j
public class OrderController {
    @Autowired
    private OrderService orderService;

    @PostMapping("submit")
    public R<String> submit(@RequestBody Orders orders){
        orderService.submit(orders);
        return R.success("下单成功");
    }

    @GetMapping("page")
    public R<Page<Orders>> pageR(Integer page, Integer pageSize, Integer number, @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime beginTime,@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime){
        Page<Orders> ordersPage=new Page<>(page,pageSize);
        orderService.page(ordersPage,new LambdaQueryWrapper<Orders>().orderByDesc(Orders::getOrderTime).like(number!=null,Orders::getNumber,number).le(endTime!=null,Orders::getOrderTime,endTime).ge(beginTime!=null,Orders::getOrderTime,beginTime));
        return R.success(ordersPage);
    }
}
